<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/classLogic.php");
include_once(constant('PROJECT_DIR')."/logic/orderLogic.php");
include_once(constant('PROJECT_DIR')."/logic/courseLogic.php");
include_once(constant('PROJECT_DIR')."/logic/deviceLogic.php");

require_once("../checkLogin.php");
$uid = $_SESSION['user'];
if($login==true)
{
		$smarty->assign("userId", $uid);
		$smarty->assign("unReadNum", $unReadNum);
}
setcookie("user",$uid);
$_GET['id']=$uid;


$LIST_NUMBER_PER_PAGE=10;
//begin to read class
$cl = new classLogic();
$ol = new orderLogic();
if(!isset($_POST['action']))
{
     $action="";
}
else
{
     $action = $_POST['action'];
}
if($action == "")
{
		if(isset($_GET['action']))
		{
			$action=$_GET['action'];
		}
}
switch($action)
{
				case "submitOrder":
		     		$menuSelected="mySubmit";
		     		$cid=$_GET['courseId'];
		     		$did=$_GET['did'];
		     		$beginDate=$_GET['beginDate'];
		     		$endDate=$_GET['endDate'];
		     		$ol->submitOrder($uid, $did, $cid, $beginDate, $endDate);
				case "search":
					$begintime="";
					$endtime="";
					$courseId="";
					$type="";
					if(isset($_POST['courseId']))
					{
							$begintime=$_POST['begintime'];
							$endtime=$_POST['endtime'];
							$courseId=$_POST['courseId'];
							$type=$_POST['type'];
					}
					else
					{
							$begintime=$_GET['beginDate'];
							$endtime=$_GET['endDate'];
							$courseId=$_GET['courseId'];
							$type=$_GET['type'];
					}
					$searchResult = $ol->searchDevice($uid, $courseId, $type);
					$freeDevices=array();
					for($i=0; $i< count($searchResult); $i++)
					{
							if($ol->checkRemain($searchResult[$i]['did'], $begintime, $endtime))
							{
									array_push($freeDevices, $searchResult[$i]);
							}
					}
					//给剩余的机器补充图片资料
					$dh = new deviceLogic();
					for($i=0; $i<count($freeDevices);$i++)
					{
							$freeDevices[$i]['beginDate']=$begintime;
							$freeDevices[$i]['endDate']=$endtime;
							$pictures=$dh->get_device_pictures($freeDevices[$i]['did']);
							if(count($pictures)>0)
							{
									$freeDevices[$i]['picture']=constant('PROJECT_URL')."/".constant('PICTURE_SUBDIR')."/".$pictures[0]['picture_name'];
							}
							else
							{
									$freeDevices[$i]['picture']=constant('PROJECT_URL')."/images/default.jpg";
							}
					}

					$searchBeginTime=$begintime;
					$searchEndTime = $endtime;
					$smarty->assign("type", $type);
					$smarty->assign("freeDevices", $freeDevices);
					$smarty->assign("searchBeginTime", $searchBeginTime);
					$smarty->assign("searchEndTime", $searchEndTime);
					break;
		case "undoOrder":
					$orderId=$_GET['orderId'];
					$ol->undoOrder($orderId, $uid);
					break;

     case "addorder":
         $id=$_POST['id'];
         $name=$_POST['name'];
         $booktime=$_POST['booktime'];
         $type=$_POST['type'];
         $class=$_POST['class'];
         $order=array();
         $order["id"]=$id;
         $order["name"]=$name;
         $order["booktime"]=$booktime;
         $order["level"]=$type;
         if($class>0)
         {
             $order["classid"]=$class;
         }
         $order["createtime"]=date("Y-m-d H:i:s");
         $ol->insert_order($order);
         break;
     case "deleteorder":
     			if(!isset($_POST['order']))
     			{
     				break;
     				}
         $orders=$_POST['order'];
         for($i=0; $i<sizeof($orders); $i++)
         {
             $ol->delete_order($orders[$i]);
         }
         break;
     case "modifyorder":
         $uid=$_POST['orderid'];
         $order=array();
         $order["id"]=$_POST["orderid"];
         $order["name"]=$_POST["name"];
         $order["booktime"]=$_POST["booktime"];
         $order["level"]=$_POST["type"];
         $order["email"]=$_POST["email"];
         $order["phone"]=$_POST["phone"];
         $order["info"]=$_POST["info"];
         $order["classid"]=$_POST["class"];
         $ol->update_order($order, $uid);
         break;
     case "saveUserCourseBooktime":
     		$uid=$_POST['orderId'];
     		$cid=$_POST['courseId'];
     		$booktime=$_POST['booktime'];
     		if($booktime<0)
     		{
     			break;
     		}
     		$ol->update_order_course_booktime($uid, $cid, $booktime);
     		break;
    case "removeordercourse":
    		$uid=$_GET['id'];
     		$cid=$_GET['courseid'];
     		$ol->delete_order_course($uid, $cid);
     		break;
}
if(isset($_GET['id']))
{
     //读取所有课程
     $orderlg = new orderLogic();
     $courselg = new courseLogic();
     $courses= $orderlg->getUserCourses($uid);
     for($i=0; $i<count($courses); $i++)
     {
     		$cinfo = $courselg->get_course(array('id'=>$courses[$i]['cid']));
     		$courses[$i]['name']=$cinfo[0]['name'];
     		$courses[$i]['begindate']=$cinfo[0]['begindate'];
     		$courses[$i]['enddate']=$cinfo[0]['enddate'];
     }
     if(isset($_GET['courseId']))
     {
     		$chosenId=$_GET['courseId'];
     		$currentCourse = $courselg->get_course(array('id'=>$chosenId));
     		$booktime=$courselg->get_booktime_by_user_course($uid, $chosenId);
     		$currentCourse[0]['booktime']=$booktime;
     		$smarty->assign("currentCourse",$currentCourse[0]);
     }

     //填充课程信息
     if(count($courses)>0)
     {
     			$smarty->assign("courses", $courses);
     }
}

$ordersNumber=0;
$pass='';
if(!isset($_GET['menuSelected']))
{
		$_GET['menuSelected']="submit";
}
if($_GET['menuSelected']=="uncheck")
{
		$pass='0';
}
if($_GET['menuSelected']=="success")
{
		$pass='1';
}
if($_GET['menuSelected']=="fail")
{
		$pass='2';
}
//handling the page index

$ordersNumber=$ol->getOrdersNumberByUserId($uid,$pass);
$pageIndex=1;
$totalPageNumber=ceil($ordersNumber/$LIST_NUMBER_PER_PAGE);
if(isset($_GET['pageIndex']))
{
     $pageIndex=$_GET['pageIndex'];
     if($pageIndex<1 || $pageIndex > $totalPageNumber)
     {
         $pageIndex=1;
     }
}
$startItem=($pageIndex-1)*$LIST_NUMBER_PER_PAGE;
$ItemNumber=$LIST_NUMBER_PER_PAGE;

$leftPageIndex=$pageIndex<=1?1:$pageIndex-1;
$rightPageIndex=$pageIndex>=$totalPageNumber?$totalPageNumber:$pageIndex+1;
$leftMostPageIndex=$pageIndex<=5?1:$pageIndex-5;//左边显示5个页面索引
$rightMostPageIndex=$totalPageNumber<$pageIndex+5?$totalPageNumber:$pageIndex+5;//右边显示5个页面索引
$smarty->assign("leftMostPageIndex",$leftMostPageIndex);
$smarty->assign("rightMostPageIndex",$rightMostPageIndex);
$smarty->assign("leftPageIndex",$leftPageIndex);
$smarty->assign("rightPageIndex",$rightPageIndex);
$smarty->assign("pageIndex",$pageIndex);
$smarty->assign("ordersNumber", $ordersNumber);
$allorders=$ol->getOrdersByUserId($uid, $pass, "limit $startItem,$ItemNumber");

//机器补充图片资料
					$dh = new deviceLogic();
					for($i=0; $i<count($allorders);$i++)
					{
							$pictures=$dh->get_device_pictures($allorders[$i]['did']);
							if(count($pictures)>0)
							{
									$allorders[$i]['picture']=constant('PROJECT_URL')."/".constant('PICTURE_SUBDIR')."/".$pictures[0]['picture_name'];
							}
							else
							{
									$allorders[$i]['picture']=constant('PROJECT_URL')."/images/default.jpg";
							}
					}
$smarty->assign("orders",$allorders);
//handling page Index end

//坐标菜单信息
if(isset($_GET['menuSelected']))
{
		$menuSelected=$_GET['menuSelected'];
}
else
{
		$menuSelected="submit";
}
$smarty->assign("menuSelected", $menuSelected);

$ordersnumber = $ol->get_order_num();
$smarty->assign("ordernumber", $ordersnumber);

$allclasses = $cl->get_class('',"order by id desc", "");
$smarty->assign("classes", $allclasses);
$smarty->assign("projectUrl", constant('PROJECT_URL'));
$smarty->assign("currentUrl","order/order.php");
$smarty->display("order/order.html");
?>
